<?php

class RoutesController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->setLayout('layoutprofile');
    }

    public function indexAction()
    {
    }

    public function statusAction()
    {
        $time = new Application_Model_Routes();
    }

    public function viewmapAction()
    {
    }

    public function savedroutesAction()
    {
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        $obj = new Application_Model_Routes();
        $routes = $obj->getSavedRoutes($id);
        $this->view->routes = $routes;
    }

    public function saverouteAction()
    {
        //текущую дату + id клиента записываем
        //как имя маршрута
        $date = date('d-m-Y H:i:s');
        $id_client  = Zend_Auth::getInstance()->getIdentity()->id;
        $save = new Application_Model_Routes();
        $data = array(
                      'id_client' => $id_client,
                      'name'      => $date.'_'.$id_client
        );        
        //получаем id и координаты всех точек маршрута
        $id_route = $save->saveRoute($data);       
        //$coordinate = $this->_getParam('coordinate');
        $coordinate = $this->getRequest()->getPost('coordinate');
        if ($coordinate != null) {
            //разбиваем попарно(latitude + longitude) 
            $var = explode('|', $coordinate);
            //проходим по каждому элементу массива
            for($i = 0; $i < count($var); $i++) { 
                //и разбиваем элемент на широту и долготу
                //$arr = explode('|', $var[$i]);
                $arr = substr($var[$i], 1);
                //при каждой итерации наполняем ими массив           
                $data = array(
                              'id_route'  => $id_route,
                              'address'   => $arr
                );
                //и записываем в базу данных
                $save->saveCoordinate($data);
            }
        } 
    }

    public function deleterouteAction()
    {
        $id = $this->_getParam('id');
        $obj = new Application_Model_Routes();
        $obj->deleteSelectRoute($id);
        $obj->deleteSelectCoordinate($id);
        $this->_helper->redirector('savedroutes', 'routes');
    }

    public function routeonmapAction()
    {
        //$this->_helper->layout->setLayout('layoutviewroutes');
        $obj = new Application_Model_Routes();
        
        $dom = new DOMDocument("1.0");
        $node = $dom->createElement("route");
        $parnode = $dom->appendChild($node);         
        
        $id = $this->_getParam('id', 0);
        $all = $obj->getRouteCoordinate($id); 
        $xml = array();
        foreach ($all as $key=> $val) {             
            $node = $dom->createElement("route");  
            $newnode = $parnode->appendChild($node);                           
            $newnode->setAttribute("address", $val['address']);             
            $xml[$key] = $dom->saveXML($node);            
        }
        $this->view->dom = $xml;
    }

    public function printAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
            //$content = $this->getRequest()->getPost('points');            
            $content = array();
            $content[0] = 1;
            $content[1] = 2;
            $content[2] = 3;                        
            if(!$content) exit("Nothing to save");
            header('Content-Type: text/plain');
            header('Content-Disposition: attachment; filename=routes.txt');
            header('Content-Transfer-Encoding: binary');
            foreach ($content as $val) {
                echo $val.'|';
            }
    }
}


